<?php
/**================================================================================
 * 파일명   : ActionClass.Class.php
 * 용도     : ActionClass 처리
 * 작성자   : 하성수
 * 연락처   : spacer.ha@gmail.com
 * 사용제한 : 
	- 이 클래스 라이브러리 사용은 무료이나, 원작자의 정보나 주석을 삭제할 수 없습니다.(자동생성되는 뷰캐시내 주석 포함)
	- 원작자가 배포한 그대로 수정되지 않은 소스는 자유롭게 배포가 가능합니다.
	- 사용자환경에 맞게 소스를 수정하여 사용은 가능하나 수정된 소스나, 수정된 소스가 포함된 프로그램을 타인에게 제공할 수 없습니다.
	- 영리/비영리/개인/기업 관계없이 모두 사용할 수 있습니다.

 * 버전     : 11.7.1
 ===============================================================================**/
require_once("Controller.Class.php");
class ActionClass extends Controller {

	var $actionClassPath 		= '';
	var $actionClassFileName 	= '';
	var $actionClassName 		= '';
	var $actionMethodName 		= '';
	var $actionObject 			= null;
	var $urlPathInfo 			= '';
	var $args 					= array();	
	var $justClassPath			= '';
	var $actionClassDefaultProperty = array();

	function ActionClass($iniControllerPath = "") {
		$this->initController($iniControllerPath);
		$this->getUrl();
		$this->initActionUriMapping();
		$this->decryptUrl();
		$this->checkUrlPathInfo();
		$this->ClassMapping($iniControllerPath);
	}	
	function ClassMapping($iniControllerPath = "") {
		$this->ParseActionClass();
		// 로그인 처리를 위한 코드
		session_start();
		if($this->actionClassName == 'Login') {
			if($_SESSION['isLogin'] == 'true') {
				$this->result_uri = $this->baseClass.'/Main';
				$this->redirect();
			}
		} else {
			if($_SESSION['isLogin'] != 'true') {
				$this->ErrorMsgMove('로그인이 필요합니다.', $this->baseClass);
			}
		}

		if(!file_exists($this->actionClassPath)) {
			$this->writeSpacProcLog();
			$this->NoClassFileError();
			exit;
		}
		require_once($this->actionClassPath);
		if(!class_exists($this->actionClassName)) {
			$this->writeSpacProcLog();
			$this->NoClassError();
			exit;
		}
		$this->getActionClassDefaultProperty();
		$this->checkDenyClassAccessControl();
		$this->actionObject = new $this->actionClassName;
		if(method_exists($this->actionObject,"initController")) {
			if($this->actionObject->loadControllerConfig != true) $this->actionObject->initController($iniControllerPath);
		}
		$this->LoadUserAutoLoadClasses();
		if(method_exists($this->actionObject,$this->actionMethodName)) {
			$this->checkMethodAccessControl();
			if($this->checkActionMethodName($this->actionMethodName) == false) {
				$this->writeSpacProcLog();
				$this->MethodNameError();
				exit;
			}
			$this->actionObject->justClassPath		= $this->justClassPath;
			$this->actionObject->actionClassPath	= $this->actionClassPath;
			$this->actionObject->actionClassName	= $this->actionClassName;
			$this->actionObject->actionMethodName	= $this->actionMethodName;
			$this->writeSpacProcLog();
			$this->actionObject->args				= $this->args;
			$this->actionObject->{$this->actionMethodName}();		
		} else {		
			array_unshift($this->args,$this->actionMethodName);
			$this->actionMethodName = $this->defaultMethodName;
			if(method_exists($this->actionObject,$this->actionMethodName)) {
				$this->checkMethodAccessControl();
				$this->actionObject->justClassPath		= $this->justClassPath;
				$this->actionObject->actionClassPath	= $this->actionClassPath;
				$this->actionObject->actionClassName	= $this->actionClassName;
				$this->actionObject->actionMethodName	= $this->actionMethodName;
				$this->writeSpacProcLog();
				$this->actionObject->args				= $this->args;
				$this->actionObject->{$this->actionMethodName}();
			} else { 
				$this->writeSpacProcLog();
				$this->NoMethodError();
				exit;
			}
		}
	}
	function LoadUserAutoLoadClasses() {
		if($this->useAutoLoadUserClass == true) {

			if(!file_exists($this->iniAutoLoadUserClass)) {
				$this->goErrorPage("Controller.ini 파일내의 [load_user_class] 항목의 \"iniAutoLoadUserClass\" 에 설정된 파일경로에 해당 파일이 존재하지 않습니다. 경로를 확인해주시기 바랍니다.");
				exit;
			}
			$AutoLoadClassesArray	= parse_ini_file($this->iniAutoLoadUserClass,true);
			foreach($AutoLoadClassesArray as $key => $value) {
				$this->autoLoadClassName		= $key;
				$this->autoLoadClassPath		= $AutoLoadClassesArray[$key]['class_path'];
				$this->allowAutoLoadClass		= $AutoLoadClassesArray[$key]['allow_auto_load_class'];
				$this->autoExecuteMethodName	= $AutoLoadClassesArray[$key]['auto_execute_method'];
				if($this->checkActionClassName($this->autoLoadClassName) == false) {
					$this->goErrorPage("AutoLoadUserClass 설정파일 ".$this->iniAutoLoadUserClass." 의 설정값중 [클래스명]이 잘못되었습니다.");
					exit;
				}
				if($this->autoLoadClassPath) {
					if(!file_exists($this->autoLoadClassPath)) {
						$this->goErrorPage("AutoLoadUserClass에 설정된 [".$this->autoLoadClassPath."]파일을 찾을 수 없습니다.");
						exit;
					}
					require_once($this->autoLoadClassPath);
					if($this->allowAutoLoadClass == true) {
						if(!class_exists($this->autoLoadClassName)) {
							$this->goErrorPage("AutoLoadUserClass에 설정된 [".$this->autoLoadClassPath."]파일에 ".$this->autoLoadClassName." 클래스가 구현되어 있지 않습니다.");
							exit;
						}
						$this->actionObject->{$this->autoLoadClassName} =  new $this->autoLoadClassName;
						if($this->autoExecuteMethodName) {
							if($this->checkActionMethodName($this->autoExecuteMethodName) == false) {
								$this->goErrorPage("AutoLoadUserClass 설정파일 ".$this->iniAutoLoadUserClass." 의 설정값중 [메소드명]이 잘못되었습니다.");
								exit;
							}
							if(!method_exists($this->actionObject->{$this->autoLoadClassName},$this->autoExecuteMethodName)) {
								$this->goErrorPage("AutoLoadUserClass에 설정된 [".$this->autoLoadClassName."]클래스에 [".$this->autoExecuteMethodName."] 메소드가 구현되어 있지 않습니다.");
								exit;
							}
							$this->actionObject->{$this->autoLoadClassName}->{$this->autoExecuteMethodName}();
						}

					}

				}

			}

		}

	}
	function ParseActionClass() {
		$this->actionClassName	= '';
		$this->actionMethodName = '';
		$urlPathInfo_Array				= preg_split("/\//",$this->urlPathInfo);
		$tmp							= array_shift($urlPathInfo_Array);
		unset($tmp);		
		$urlPathInfo_Array = $this->iniActionClassMapping($urlPathInfo_Array);
		if($this->useModelSubDir == true) {
			$newUrlPathInfo_Array			= $urlPathInfo_Array;
			$this->justClassPath			= '';
			for($i = 0;$i<count($newUrlPathInfo_Array);$i++) {
				$Loop_Value 			= $newUrlPathInfo_Array[$i];
				$Loop_ClassFileDir		= $this->modelPath . $this->justClassPath;
				$Loop_ClassFilePath 	= $Loop_ClassFileDir. "/" . $newUrlPathInfo_Array[$i]. "." . $this->classFileExt;
				if(file_exists($Loop_ClassFilePath)) {
					break;
				} else {
					$tmp						= array_shift($urlPathInfo_Array);
					$this->justClassPath		.= "/" . $tmp;
					$Loop_ClassFileDir		= $this->modelPath . $this->justClassPath;
					if(!is_dir($Loop_ClassFileDir)) {
						$this->goErrorPage("<b><font color=red>".$Loop_ClassFilePath."</font></b> file OR <b><font color=red>".$Loop_ClassFileDir."</font></b> Directory is not exists.");
						exit;
					}
				}		
			}
		}
		$this->actionClassName			= stripslashes(trim(array_shift($urlPathInfo_Array)));
		if($this->checkActionClassName($this->actionClassName) == false) {
			$this->writeSpacProcLog();
			$this->ClassNameError();
			exit;
		}
		if(count($urlPathInfo_Array)) {
			$this->actionMethodName		= stripslashes(trim(array_shift($urlPathInfo_Array)));
		} else {
			$this->actionMethodName		= $this->defaultMethodName;
		}
		if(!strlen(trim($this->actionMethodName))) $this->actionMethodName = $this->defaultMethodName;
		if(count($urlPathInfo_Array)) {
			$this->args = $urlPathInfo_Array;
		}
		$this->actionClassFileName		= $this->actionClassName. "." . $this->classFileExt;
		$this->actionClassPath			= $this->modelPath . $this->justClassPath . "/" . $this->actionClassFileName;
	}
	function iniActionClassMapping($urlPathInfo_Array) {
		if($this->useActionClassMapping == true) {
			$confActionClassMap					= parse_ini_file($this->iniActionClassMapPath,true);
			if($confActionClassMap["Class"][$urlPathInfo_Array[0]]) {
				$urlPathInfo_Array[0]			= $confActionClassMap["Class"][$urlPathInfo_Array[0]];
			}
		}
		return $urlPathInfo_Array;
	}
	function initActionUriMapping() {
		if($this->useActionUriMapping == true) {
			$confActionUriMap				= parse_ini_file($this->iniActionUriMapPath,true);
			foreach($confActionUriMap as $UriMapInfo) {
				if($this->urlPathInfo == $UriMapInfo['requestUri']) {
					$this->urlPathInfo = $UriMapInfo['actionUri'];
					break;
				}
			}
		}
	}
	function getActionClassDefaultProperty() {
		$this->actionClassDefaultProperty = get_class_vars($this->actionClassName);
	}	
	function checkDenyClassAccessControl() {
		if($this->actionClassDefaultProperty['DenyClassAccess']) { 
			$this->denyClassAccessError();
			exit;
		}		
	}	
	function checkMethodAccessControl() {		
		if($this->actionClassDefaultProperty['MethodAccessControlType'] == 'deny') {
			if(is_array($this->actionClassDefaultProperty['AccessMethods'])) { 	
				if(in_array($this->actionMethodName,$this->actionClassDefaultProperty['AccessMethods'])) { 
					$this->denyMethodAccessError();
					exit;
				}
			}
		}
		if($this->actionClassDefaultProperty['MethodAccessControlType'] == 'allow') {
			if(is_array($this->actionClassDefaultProperty['AccessMethods'])) { 	
				if(!in_array($this->actionMethodName,$this->actionClassDefaultProperty['AccessMethods'])) { 
					$this->denyMethodAccessError();
					exit;
				}
			}
		}		
	}
}
$ActionClass = new ActionClass($iniControllerPath);
?>
